package com.example

import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.calllogging.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.io.File

fun main() {
    embeddedServer(Netty, port = 8899) {
        install(CallLogging)
        install(jwtPlugin)
        routing {
            get("/") {
                call.respondText("index page", contentType = ContentType.Text.Plain)
            }
            get("/a") {
                call.respondText("Hello aaaa!", contentType = ContentType.Text.Plain)
            }
        }
    }.start(wait = true)

}

val jwtPlugin = createApplicationPlugin(name = "jwtPlugin") {
    onCall { call ->
        val jwt = call.request.header("jwt")
        println(jwt)
        if (jwt.equals("yk")) {
            call.respond(HttpStatusCode.OK, "你是游客")
            return@onCall
        }

    }
}